from dataclasses import dataclass

from src.application.user.uow import UserUoW
from src.domain.user.value_objects import TelegramUserId


@dataclass(frozen=True)
class CreateAllowedIp:
    tg_user_id: int
    ip_address: str


class CreateAllowedIpHandler:
    """Добавление allowed_ip пользователя"""

    def __init__(self, *, uow: UserUoW) -> None:
        self._uow = uow

    async def __call__(self, command: CreateAllowedIp) -> None:
        user_id = TelegramUserId(value=command.tg_user_id)
        user = await self._uow.user_repo.acquire_user_by_id(user_tg_id=user_id)
        user.create_allowed_ip(user.id, command.ip_address)
        print(user)

        try:
            await self._uow.user_repo.update_user(user=user)
        except Exception as err:
            await self._uow.rollback()
            raise err

        await self._uow.commit()
